package com.base.cn.platform.os.controller.course.classes.topic;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.classes.topic.like.ClassesTopicLikeService;
import com.base.cn.platform.os.service.course.classes.topic.report.ClassesTopicReportService;
import com.base.cn.platform.os.service.course.classes.topic.type.ClassesTopicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 班级话题接口Controller
 *
 * @author s.li
 * @create 2018-09-05-19:05
 */
@RestController
@RequestMapping("/api/topic")
public class ApiClassesTopicController extends ApiBaseController {

    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private ClassesTopicLikeService classesTopicLikeService;
    @Autowired
    private ClassesTopicReportService classesTopicReportService;
    @Autowired
    private CommonConstants commonConstants;
    @Autowired
    private ClassesTopicTypeService classesTopicTypeService;

    /**
     * 我的话题列表接口
     * @param userId 用户ID
     * @param which 1.全部话题 2.发表的话题 3.参与的话题，4.转发的话题
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/myTopicList")
    public Map<String,Object> myTopicList(@RequestParam("userId") BigDecimal userId,
                                          @RequestParam(value = "which",required = false,defaultValue = "1") int which,
                                          @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){

        Map<String, Object> dataMap = classesTopicService.findClassesPersonalTopicPage(userId, currentPage, null, which, true, true, false);
        return ResultUtil.SUCCESS(dataMap);
    }


    /**
     * 所有话题接口
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/findAllTopicList")
    public Map<String,Object> findAllTopicList(@RequestParam("userId") BigDecimal userId,
                                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        if(!DataUtil.idIsNotNull(userId)){
            return ResultUtil.ERROR("请登录后再操作");
        }
        Map<String, Object> dataMap = classesTopicService.findClassesPersonalTopicPage(userId, currentPage, 10, 1, true, false, false);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 获取班级下的话题列表
     * @param classId 班级ID
     * @param currentPage  页码
     * @return Map<String,Object>
     */
    @RequestMapping("/findClassesTopicList")
    public Map<String,Object> findClassesTopicList(@RequestParam("classId") BigDecimal classId,
                                                   @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("classesId",classId);
        params.put("status",1);
        Map<String,Object> dataMap = classesTopicService.findClassesTopicListPage(params,currentPage,10,0,true,false,false);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 保存话题
     * @param userId 用户ID
     * @param title 话题标题
     * @param content 话题内容
     * @param classesId 话题对应的班级
     * @param classify 话题类型，1.原创 2.讨论 3，疑问 4.活动 5.其他
     * @param topicImages 创建话题时上传的图片，最多可上传四张，可以不传，这个图片路径间以“,”隔开
     * @param lookPermission 浏览权限 1.自由浏览 2.登录后浏览 3.仅本班同学,默认1
     * @param replyPermission 回复权限 1.自由回复 2.仅本班同学，默认1
     * @return Map<String,Object>
     */
    @RequestMapping("/saveClassesTopic")
    public Map<String,Object> saveClassesTopic(@RequestParam("userId") BigDecimal userId,
                                               @RequestParam("title") String title,
                                               @RequestParam("content") String content,
                                               @RequestParam("classesId") BigDecimal classesId,
                                               @RequestParam("classify") int classify,
                                               @RequestParam(value = "topicImages",required = false) String topicImages,
                                               @RequestParam(value = "lookPermission",required = false,defaultValue = "1") int lookPermission,
                                               @RequestParam(value = "replyPermission",required = false,defaultValue = "1") int replyPermission){
        Map<String,Object> classesTopic = new HashMap<>();
        classesTopic.put("title",title);
        classesTopic.put("content",content);
        classesTopic.put("classify",classify);
        classesTopic.put("topicImages",topicImages);
        classesTopic.put("lookPermission",lookPermission);
        classesTopic.put("replyPermission",replyPermission);
        classesTopic.put("createUserId",userId);
        classesTopic.put("source",1);
        classesTopic.put("status",1);
        return classesTopicService.saveClassesTopic(classesTopic,String.valueOf(classesId));
    }

    /**
     * 获取话题详情
     * @param topicId 话题ID
     * @param userId  当前登录的用户ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/topicInfo")
    public Map<String,Object> topicInfo(@RequestParam("topicId") BigDecimal topicId,
                                        @RequestParam("userId") BigDecimal userId){
        Map<String,Object> dataMap = new HashMap<>();
        // 话题
        Map<String, Object> topic = classesTopicService.findClassesTopicById(topicId, true, true, true, true);
        dataMap.put("topic",topic);

        dataMap.put("liked",false);
        dataMap.put("joined",false);
        if(DataUtil.idIsNotNull(userId)){
            //是否点赞过
            boolean liked = classesTopicLikeService.checkUserLikeTopic(userId,topicId);
            dataMap.put("liked",liked);
            // 是否加入班级
            Number classesId = (Number) topic.get("classesId");
            boolean joined = classesMemberService.joined(new BigDecimal(classesId.toString()), userId);
            dataMap.put("joined",joined);
        }

        String shareUrl= commonConstants.get(CommonConstants.MOBILE_PATH) +"/mobile/class/topic/info?id="+topicId+"&shareKey="+ MD5.getMD5(userId.toString())+"_"+userId;
        dataMap.put("shareUrl",shareUrl);

        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 添加点赞记录
     * @param userId 用户ID
     * @param topicId 话题 ID
     * @return Map<String,Object>
     */
    @RequestMapping("/likeTopic")
    public Map<String,Object> likeTopic(@RequestParam("userId") BigDecimal userId,
                                        @RequestParam("topicId") BigDecimal topicId){
        return classesTopicLikeService.saveClassesTopicLike(topicId,userId);
    }

    /**
     * 保存话题举报内容
     * @param topicId 话题ID
     * @param userId 举报的用户ID
     * @param type 举报类型 1.广告及垃圾信息 2.抄袭或未授权转载 3.其他
     * @param content 举报内容
     * @return Map<String,Object>
     */
    @RequestMapping("/saveReportTopic")
    public Map<String,Object> saveReportTopic(@RequestParam("topicId") BigDecimal topicId,
                                              @RequestParam("userId") BigDecimal userId,
                                              @RequestParam("type") int type,
                                              @RequestParam("content") String content){
        Map<String,Object> params = new HashMap<>();
        params.put("topicId",topicId);
        params.put("userId",userId);
        params.put("type",type);
        params.put("content",content);
        return classesTopicReportService.saveClassesTopicReport(params);
    }

    /**
     * 查询班级话题类型
     * @param classesId  班级id
     * @return 班级话题类型
     */
    @RequestMapping("/getClassesTopicType")
   public Map<String,Object> getClassesTopicType(@RequestParam("classesId")BigDecimal  classesId){
        List<Map<String, Object>> classesTopicType = classesTopicTypeService.getClassesTopicType(classesId);
        return ResultUtil.SUCCESS(classesTopicType);
    }
}
